package com.zhou.JavaAndroidFirm.widget;

import android.support.v4.content.ContextCompat;
import android.text.Layout;
import android.text.Selection;
import android.text.Spannable;
import android.text.method.BaseMovementMethod;
import android.text.method.Touch;
import android.text.style.BackgroundColorSpan;
import android.text.style.ClickableSpan;
import android.view.MotionEvent;
import android.widget.TextView;

import com.zhou.JavaAndroidFirm.R;
import com.zhou.JavaAndroidFirm.base.BaseApplication;

/**
 * @author Administrator
 * @date 2018/7/24 0024
 * @des
 */
public class CircleMovementMethod extends BaseMovementMethod {
    public final String TAG = CircleMovementMethod.class.getSimpleName();
    public final static int DEFAULT_COLOR = R.color.transparent;
    private int mTextViewBgColorId;
    private int mClickableSpanBgClorId;


    private BackgroundColorSpan mBgSpan;
    private ClickableSpan[] mClickLinks;

    public boolean isPassToTv() {
        return isPassToTv;
    }

    /**
     * true：响应textview的点击事件， false：响应设置的clickableSpan事件
     */
    public void setPassToTv(boolean passToTv) {
        isPassToTv = passToTv;
    }

    private boolean isPassToTv = true;

    public CircleMovementMethod() {
        mTextViewBgColorId = DEFAULT_COLOR;
        mClickableSpanBgClorId = DEFAULT_COLOR;
    }

    public CircleMovementMethod(int clickableSpanBgClorId) {
        mClickableSpanBgClorId = clickableSpanBgClorId;
        mTextViewBgColorId = DEFAULT_COLOR;
    }

    public CircleMovementMethod(int clickableSpanBgClorId, int textViewBgColorId) {
        mClickableSpanBgClorId = clickableSpanBgClorId;
        mTextViewBgColorId = textViewBgColorId;
    }

    @Override
    public boolean onTouchEvent(TextView widget, Spannable text, MotionEvent event) {
        int action = event.getAction();
        //如果按下
        if (action == MotionEvent.ACTION_DOWN) {
            //获取按下的x和y坐标
            int x = (int) event.getX();
            int y = (int) event.getY();
            //用该坐标减去文字隔左边界和顶部的距离
            x -= widget.getTotalPaddingBottom();
            y -= widget.getTotalPaddingTop();
            //getScrollX() 就是当前view的左上角相对于母视图的左上角的X轴偏移量。
            x += widget.getScrollX();
            y += widget.getScrollY();

            Layout layout = widget.getLayout();
            //获取当前y坐标属于第几行
            int line = layout.getLineForVertical(y);
            //得到触摸点在某一行水平方向上的偏移量。
            int off = layout.getOffsetForHorizontal(line, x);
            //获取当前位置是否是span
            mClickLinks = text.getSpans(off, off, ClickableSpan.class);
            if (mClickLinks.length > 0) {
                //点击的是span区域 不要传递点击事件
                setPassToTv(false);
                //设置选中区域
                Selection.setSelection(text, text.getSpanStart(mClickLinks[0]), text.getSpanEnd(mClickLinks[0]));
                //设置点击区域的背景色
                mBgSpan = new BackgroundColorSpan(ContextCompat.getColor(BaseApplication.getAppContext(), mClickableSpanBgClorId));
                text.setSpan(mBgSpan
                        , text.getSpanStart(mClickLinks[0])
                        , text.getSpanEnd(mClickLinks[0])
                        , Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

            } else {
                setPassToTv(true);
                //如果点击不是span区域，由textview空间去响应背景色
                widget.setBackgroundResource(mTextViewBgColorId);
            }
        } else if (action == MotionEvent.ACTION_UP) {
            //如果点击下去的是span区域
            if (mClickLinks.length > 0) {
                //由该span响应该点击事件
                mClickLinks[0].onClick(widget);
                if (mBgSpan != null) {
                    //如果点击背景色存在移除掉
                    text.removeSpan(mBgSpan);
                }
            } else {
                if (mBgSpan != null) {
                    //如果点击背景色存在移除掉
                    text.removeSpan(mBgSpan);
                }
            }
            //移除选中区域
            Selection.removeSelection(text);
            //设置为默认初始背景颜色
            widget.setBackgroundResource(DEFAULT_COLOR);
        } else if (action == MotionEvent.ACTION_MOVE) {
        } else {
            if (mBgSpan != null) {
                text.removeSpan(mBgSpan);
            }
            widget.setBackgroundResource(DEFAULT_COLOR);
        }
        return Touch.onTouchEvent(widget, text, event);
    }
}
